package com.gzl.gg.config;

import com.gzl.gg.interceptor.EncodingInterceptor;
import com.gzl.gg.interceptor.MyWebRequestInterceptor;
import com.gzl.gg.interceptor.ZKLockInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {


    @Bean
    public MyWebRequestInterceptor myWebRequestInterceptor() {
        return new MyWebRequestInterceptor();
    }

    @Bean
    public EncodingInterceptor encodingInterceptor() {
        return new EncodingInterceptor();
    }

    @Bean
    public ZKLockInterceptor zKLockInterceptor() {
        return new ZKLockInterceptor();
    }
    /**
     * 拦截器
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {

        /**
         * 分布式锁 拦截
         */
        registry.addInterceptor(zKLockInterceptor()).addPathPatterns("/testContr/**");
        /**
         * 編碼攔截
         */
        registry.addInterceptor(encodingInterceptor()).addPathPatterns("/**");
        /**
         * MWebRequestInterceptor
         */
        registry.addWebRequestInterceptor(myWebRequestInterceptor()).addPathPatterns("/**");
        /**
         * 登录拦截
         */
//        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/sys/**");

        super.addInterceptors(registry);

    }
    /**
     * 发现如果继承了WebMvcConfigurationSupport，则在yml中配置的相关内容会失效。
     * 需要重新指定静态资源
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

        super.addResourceHandlers(registry);
    }

    /**
     * 配置servlet处理
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}